package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.pigspring.bean.po.Goods;
import com.zjitc.pigspring.bean.po.Order;
import com.zjitc.pigspring.bean.po.Shop;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.RedisSetNull;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.CollectService;
import com.zjitc.pigspring.service.GoodsService;
import com.zjitc.pigspring.service.OrderService;
import com.zjitc.pigspring.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CollectService collectService;

    @GetMapping("/getAll")
    public Result<List<Goods>> getAll(){
        List<Goods> goodsList = goodsService.getGoodsList();
        if (goodsList!=null){
            return Result.success(goodsList);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @ResponseBody
    @GetMapping("/getTop")
    public Result<List<Goods>> getTop(){
        List<Goods> goods = goodsService.getTops();
        System.out.println(goods);
        if (goods==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(goods);
        }
    }

    @GetMapping("/selectGood/{id}")
    public Result<Goods> getGoodsById(@PathVariable Integer id){
        Goods goods = goodsService.getById(id);
        if (goods!=null){
            return Result.success(goods);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }


    @GetMapping("/selectGoods/{id}")
    public Result<List<Goods>> getGoodBySeller(@PathVariable Integer id){
        List<Goods> list = goodsService.GoodsListBySeller(id);

        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/selectGoodsByType/{id}")
    public Result<List<Goods>> getGoodByType(@PathVariable Integer id){
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<Goods>()
                .eq(id!=null,Goods::getTypeId,id);
        List<Goods> list = goodsService.list(goodsLambdaQueryWrapper);

        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PutMapping("/updateOne")
    public Result updateGoods(@RequestBody Goods goods){
        boolean flag = goodsService.updateById(goods);
        if (flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/addOne")
    public Result addGoods(@RequestBody Goods goods){
        boolean save = goodsService.save(goods);
        if (save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @RedisSetNull
    @Transactional
    @DeleteMapping("/deleteOne/{id}")
    public Result deleteGoods(@PathVariable Integer id){
        boolean flag = goodsService.removeById(id);
        if (flag){

            boolean flag1 = orderService.deleteByGoodId(id);
            boolean flag2 = shopService.deleteByGoodId(id);
            boolean flag3 = collectService.deleteByGoodId(id);

            if (flag1 && flag2 && flag3) {
                return Result.success(Codes.DELETE_YES);
            }else{
                return Result.error(Codes.DELETE_NO);
            }
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @RedisSetNull
    @Transactional
    @PostMapping("/deleteAll")
    public Result deleteGoodsAll(@RequestBody List<Integer> ids){
        boolean flag = goodsService.removeBatchByIds(ids);
        if (flag){
            boolean flag1 = true;
            boolean flag2 = true;
            boolean flag3 = true;
            for (Integer id : ids) {
                flag1 = shopService.deleteByGoodId(id);
                flag2 = orderService.deleteByGoodId(id);
                flag3 = collectService.deleteByGoodId(id);
            }
            if (flag1 && flag2 && flag3){
                return Result.success(Codes.DELETE_YES);
            }else{
                return Result.error(Codes.DELETE_NO);
            }
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Goods>> conditionQueryGoods(@RequestBody Goods goods){

        List<Goods> list = goodsService.goodsQuery(goods    );
        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/conditionQueryBySeller")
    public Result<List<Goods>> conditionQueryGoodsBySeller(@RequestBody Goods goods){

        List<Goods> list = goodsService.goodsQueryBySeller(goods);
        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }


}